package com.wu.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.mapper.CommentReportMapper;
import com.wu.mapper.CommentsMapper;
import com.wu.pojo.CommentReport;
import com.wu.pojo.model.CommentModel;
import com.wu.service.CommentReportService;
import com.wu.service.CommentsService;
import com.wu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.wu.constant.Constant.*;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-13 15:54
 */
@Service
public class CommentReportServiceImpl extends ServiceImpl<CommentReportMapper, CommentReport> implements CommentReportService {

    @Autowired
    private CommentReportMapper commentReportMapper;

    @Autowired
    private MyUtil myUtil;

    @Autowired
    private CommentsService commentsService;


    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public void insertCommentReport(String articleId, String commentId, String commentContent, String reportReason, String added) {
        String reportId = IdWorker.get32UUID();
        String reportDate = myUtil.getCurrentDetailedTime();
        CommentReport commentReport = new CommentReport();
        commentReport.setReportId(reportId);
        commentReport.setArticleId(articleId);
        commentReport.setCommentId(commentId);
        commentReport.setCommentContent(commentContent);
        commentReport.setReportReason(reportReason);
        commentReport.setAdded(added);
        commentReport.setReportDate(reportDate);
        Integer integer = commentReportMapper.insertCommentReport(commentReport);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "举报评论失败");
        }
    }

    @Override
    public Map<String, Object> getCommentReportById(String reportId) {
        CommentReport report = commentReportMapper.getCommentReportById(reportId);
        Map<String, Object> map = new HashMap<>();
        map.put(ARTICLE_REPORT, report);
        return map;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteCommentReportById(String reportId, String type) {
        if (!StringUtils.isEmpty(type)) {
            //如果不为空就说明要删除  举报   评论及子评论
            CommentReport commentReport = commentReportMapper.getCommentReportById(reportId);
            String commentId = commentReport.getCommentId();
            commentsService.deleteCommentsByCommentId(commentId);
        } else {
            //如果为空就说明此举报不成立，直接删除举报即可
            commentReportMapper.deleteCommentReportById(reportId);
        }
    }

    /**
     * 通过父评论Id获取子评论
     *
     * @param parentCommentsId
     * @return
     */
    private List<CommentModel> getSons(String parentCommentsId) {
        return commentsMapper.getCommentByParentId(parentCommentsId);
    }

    @Override
    public Map<String, Object> getAllCommentReport(Integer curPage) {
        Map<String, Object> map = new HashMap<>();
        Integer count = commentReportMapper.getAllCommentReportCount();
        map.put(COUNT, count);
        int totalPage = (count + 15 - 1) / 15;
        if (totalPage == 0) {
            map.put(TOTAL_PAGE, 0);
            map.put(COMMENT_REPORTS, new ArrayList<>());
            return map;
        }
        if (curPage < 1) {
            curPage = 1;
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }
        map.put(TOTAL_PAGE, totalPage);
        List<CommentReport> reportList = commentReportMapper.getAllCommentReport((curPage - 1) * 15);
        reportList = reportList.stream().peek(report -> {
            String added = report.getAdded();
            if (!StringUtils.isEmpty(added)) {
                String s = added.substring(0, Math.min(added.length(), 50));
                report.setReportReason(s);
            }
        }).collect(Collectors.toList());
        map.put(COMMENT_REPORTS, reportList);
        return map;
    }
}
